<template>
 <div class="user-info-wrap">
   <div class="user-info">
     <img class="avaitor" :src="userData.picture" alt="">
     <span>{{userData.nickName}}</span>
   </div>
   <!-- @提到我的 -->
  <router-link to="/atMe" class="about" v-if="userData.isMe">
    @提到我的 ({{ atCount }})
  </router-link>
   <div class="user-info-tool" v-else>
    <button class="btn btn-primary" @click="follow" v-if="!userData.amIFollow">关注</button>
    <button class="btn btn-light" @click="unFollow" v-else>取消关注</button>
   </div>
 </div>
</template>
<script>
import { userFollow, userUnFollow } from 'api/userRelation'
import { getAtMeCount } from 'api/utils'
export default {
  props: {
    userData: {
      type: Object,
      default () {
        return {}
      }
    },
    amIFollow: {
      type: Boolean,
      default: false
    }
  },
  name: 'userInfo',
  data () {
    return {
      atCount: 0
    }
  },
  methods: {
    /**
     * 关注用户
     * @method follow
     */
    async follow () {
      this.userData.amIFollow = true
      let result = await userFollow(this.userData.id)
      if (result.errno !== 0) {
        alert(result.message)
        return ''
      }
    },
    /**
     * 取消关注用户
     * @method unFollow
     */
    async unFollow () {
      this.userData.amIFollow = false
      let result = await userUnFollow(this.userData.id)
      if (result.errno !== 0) {
        alert(result.message)
        return ''
      }
    }
  },
  watch: {
    async userData (user) {
      this.atCount = await getAtMeCount(user.id)
    }
  }
}
</script>
<style lang="scss" scoped>
.user-info-wrap{
  max-width: 600px;
  .user-info{
    line-height: 48px;
  }
  .about{
    line-height: 60px;
    display: inline-block;
  }
  .user-info-tool{
    margin-bottom: 15px;
    margin-top: 10px;
  }

}
a {
    color: #007bff;
    text-decoration: none;
    background-color: transparent;
}
</style>
